package com.perfma.xtest.tool.word;

import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;

/**
 * @author: junhui.si
 * @date: 2021-08-31 15:42
 **/
public class LocalDateDemo {
    public static void main(String[] args) {
        LocalDate newDate = LocalDate.of(2021, 8, 2);
        LocalDate oldDate = LocalDate.of(2021, 8, 1);
//        System.out.println("OldDate：" + oldDate);
//
//        Period p = Period.between(oldDate, newDate);
//        System.out.printf("目标日期距离今天的时间差：%d 年 %d 个月 %d 天\n", p.getYears(), p.getMonths(), p.getDays());
//
//        System.out.println(getFirstDayOfMonth(new Date()));
//        System.out.println(getFirstDayOfYear(new Date()));
//
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(new Date());
//        int month = calendar.get(Calendar.MONTH) + 1;
//        month = 3 * (month / 3);
//        System.out.println(month);


//        Interval interval = new Interval(Date.from(oldDate.atStartOfDay(ZoneId.systemDefault()).toInstant()).getTime(),
//                Date.from(newDate.atStartOfDay(ZoneId.systemDefault()).toInstant()).getTime());
//        Period period = interval.toPeriod(PeriodType.months());
//        System.out.println(period.getMonths());
//        period = interval.toPeriod(PeriodType.years());
//        System.out.println(period.getYears());
//        period = interval.toPeriod(PeriodType.days());
//        System.out.println(period.getDays());

//        for (int i = 0; i < 12; i++) {
//            System.out.println(3 * (i / 3));
//        }

        System.out.println(formatTime(   60 * 60 +6));
    }

    public static Date getFirstDayOfYear(Date time) {
        Calendar calendarInstance = Calendar.getInstance();
        calendarInstance.setTime(time);
        calendarInstance.set(Calendar.MONTH, 0);
        calendarInstance.set(Calendar.DAY_OF_MONTH, 1);
        return calendarInstance.getTime();
    }

    public static Date getFirstDayOfMonth(Date time) {
        Calendar calendarInstance = Calendar.getInstance();
        calendarInstance.setTime(time);
        calendarInstance.set(Calendar.DAY_OF_MONTH, 1);
        return calendarInstance.getTime();
    }

    public static String formatTime(int seconds) {
        int day = seconds / (3600 * 24);
        int hour = seconds / 3600;
        int minis = seconds / 60;
        int remainSeconds = seconds % 60;

        if (day == 0 && hour == 0 && minis == 0) {
            return remainSeconds + "秒";
        } else if (day == 0 && hour == 0 && minis > 0) {
            return minis + "分钟" + parse(remainSeconds, "秒");
        } else if (day == 0 && hour > 0) {
            minis = minis - hour * 60;
            return hour + "小时" + parse(minis, "分钟") + parse(remainSeconds, "秒");
        } else if (day > 0) {
            hour = hour - day * 24;
            minis = minis - hour * 60 - day * 24 * 60;
            return day + "天" + parse(hour, "小时") + parse(minis, "分钟") + parse(remainSeconds, "秒");
        }

        return "";
    }

    private static String parse(int value, String suffix) {
        return value == 0 ? "" : value + suffix;
    }
}
